package cn.edu.ctbu.labtest.webapi;

import cn.edu.ctbu.labtest.core.PageUntils;
import cn.edu.ctbu.labtest.domain.Teacher;
import cn.edu.ctbu.labtest.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/webapi/teacher")
public class TeacherRestController {

    @Autowired
    private TeacherService teacherService;

    /*
    * 读取的url：/webapi/student/list
    *
    * */

    @GetMapping("/list")
    public List<Teacher> getAll(){
        List<Teacher> teachers = teacherService.findAll();


        return teachers;

    }

    /**
     * 分页，读取全部内容
     * @return
     */
    @GetMapping("/getbypage")
    public PageUntils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                                @RequestParam(value = "size",defaultValue = "10") Integer size,
                                @RequestParam(value = "name",defaultValue = "")String name){

        //按id 倒序
        Sort sort = Sort.by(Sort.Direction.DESC,"id");

        Page<Teacher> teacherPage;

        if (StringUtils.isEmpty(name)){
            Pageable pageable = PageRequest.of(page,size,sort);
            teacherPage = teacherService.findAll(pageable);
        }else{
            Teacher teacher = new Teacher();
            teacher.setName(name);

            Pageable pageable = PageRequest.of(0,10,sort);

            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());//全部模糊查询
            Example<Teacher> example = Example.of(teacher,matcher);

            teacherPage = teacherService.findAll(example,pageable);
        }

            PageUntils pageUntils= new PageUntils(teacherPage.getContent(), teacherPage.getTotalElements()) ;
            return pageUntils;

    }

     /*
     * 读取的url：/webapi/student/list/get/id
     *
     * */
    @GetMapping("/get/{id}")
    public Teacher get(@PathVariable int id){

        Teacher teacher = teacherService.getById(id);

        teacher.setPassword("");

        return teacher;

    }


    /**
     * 新增学生的方法,/webapi/student/insert
     * @param teacher
     * @return
     */
    @PostMapping("/insert")
    public Teacher insert(Teacher teacher){

        Teacher teacher1 = teacherService.insert(teacher);

        return teacher1;

    }

    /**
     * 更新学生的方法,/webapi/student/update
     * @param teacher
     * @return
     */
    @PutMapping("/update")
    public Teacher update(Teacher teacher){

        //读取旧的数据
        Teacher oldteacher = teacherService.getById(teacher.getId());

        if (StringUtils.isEmpty(teacher.getPassword())){
            //如果不设置新密码，就按照旧的密码
            teacher.setPassword(oldteacher.getPassword());
        }

        teacher = teacherService.update(teacher);

        return teacher;

    }

    /**
     * 删除学生的方法,/webapi/teacher/delete
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id) {

        teacherService.delete(id);

    }
}
